﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using OpSystem.manage;
using OpSystem.common;
using OF.Utility;
using OF.Component.BLL;
using OF.Component.Model;

namespace CG.OpSystemNew.Manage.webinfo
{
    public partial class AuthorityUserAdd : BasePage
    {
        protected string Message = "";

        public int UserId {
            get {
                return RequestUtility.GetQueryInt("id", -1);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPageCode("10104");
            if (!Page.IsPostBack)
            {
                if (UserId>0)
                {
                    AuthorityUserInforInfo info = AuthorityUserInforOperate.GetByID(UserId);
                    if (info!=null)
                    {
                        this.txtName.Text = info.UserName;
                        this.txtEjLoginName.Text = info.LoginName;
                        this.txtEjLoginName.ReadOnly = true;
                        txtMobile.Text = info.ContactMobile;
                    }
                }
            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strMessage = "用户修改成功！";
            if (UserId > 0)
            {
                AuthorityUserInforInfo info = AuthorityUserInforOperate.GetByID(UserId);
                if (info != null)
                {
                  //  OpUserInforInfo oldinfo = ObjectCopy<OpUserInforInfo>.DeepCopy(info);
                    info.ContactMobile = txtMobile.Text.Trim();
                    info.UserName = this.txtName.Text.Trim();
                    AuthorityUserInforOperate.Update(info);
                    //    OpOperationLogOperate.SetLog(oldinfo, info, OpUserID, OpUserInfo.OpUserName, (short)LogType.OpUserManage,info.OpUserID);
                    Message = " BMCW.msg(\"" + strMessage + "\",\"" + GetPageUrl("Authorityuserlist.aspx") + "\",true);";
                }
                else
                {
                    Message = " BMCW.alert('非法操作！');";
                }
            }
            else
            {
                AuthorityUserInforInfo info = AuthorityUserInforOperate.QuerySingle(a => a.IsActive == 1 && a.LoginName == this.txtEjLoginName.Text);
                if (info == null)
                {
                    info = new AuthorityUserInforInfo();
                    info.LoginName = this.txtEjLoginName.Text;
                    info.ContactMobile = txtMobile.Text.Trim();
                    info.PassWord = SecurityUtility.EncryptAsc(this.txtNewPass.Text);
                    info.LastLoginDate = DateTime.Now;
                    info.LoginCount = 0;
                    info.IsActive = 1;
                    info.UserName = this.txtName.Text;
                    info.CreatedDate = DateTime.Now;
                    info.UpdateTime = DateTime.Now;
                    int opUserId = AuthorityUserInforOperate.Insert(info);
                    //info.OpUserID = opUserId;
                    //OpOperationLogOperate.SetLog(null, info, OpUserID, OpUserInfo.OpUserName, (short)LogType.OpUserManage, opUserId);
                    strMessage = "账户添加成功！";
                    Message = " BMCW.msg(\"" + strMessage + "\",\"" + GetPageUrl("Authorityuserlist.aspx") + "\",true);";
                }
                else
                {
                    Message = " BMCW.alert('登陆名已经存在，请重新输入！');";
                }

            }

        }

    }
}